package com.igeek.health.config;

import cn.hutool.core.util.ObjectUtil;
import com.igeek.health.entity.User;
import com.igeek.health.exception.HealthException;
import com.igeek.health.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/9/14  9:36
 */
@Service
public class SpringSecurityDetailsService implements UserDetailsService {
    @Autowired
    private IUserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User userInfo = userService.findUserByUsername(username);
        if(ObjectUtil.isEmpty(userInfo)){
            throw new HealthException("不存在当前用户信息！");
        }
        LoginUser loginUser = new LoginUser(userInfo);
        return loginUser;
    }
}
